package com.example.demos.service.impl;

import com.example.demos.dto.EmpAddDTO;
import com.example.demos.dto.EmpDTO;
import com.example.demos.dto.EmpEditDTO;
import com.example.demos.dto.EmpQueryDTO;
import com.example.demos.entity.Dep;
import com.example.demos.entity.Emp;
import com.example.demos.mapper.EmpMapper;
import com.example.demos.mapper.MenuMapper;
import com.example.demos.service.EmpService;
import com.example.demos.util.EncryptionPassWord;
import com.example.demos.util.PageBean;
import com.example.demos.util.PageParam;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

import static com.example.demos.util.EncryptionPassWord.*;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public PageBean<List<EmpDTO>> select(PageParam pageParam, EmpQueryDTO empQueryDTO) {
        Page<EmpDTO> page = PageHelper.startPage(pageParam.getPage(), pageParam.getRows());
        empMapper.select(empQueryDTO);
        PageBean<List<EmpDTO>> listPageBean = new PageBean<>(page.getTotal(), page.getResult());
        return listPageBean;
    }

    @Override
    public List<Dep> selectDepList() {
        return empMapper.selectDepList();
    }

    @Override
    public int delete(BigInteger id) {
        List<BigInteger> roleIds = empMapper.selectRoleById(id);
        if (roleIds == null || roleIds.isEmpty()) return empMapper.delete(id);
        return -1;
    }

    @Override
    public List<Emp> selectForList() {
        return empMapper.selectForList();
    }

    @Override
    @Transactional
    public int insert(EmpAddDTO empAddDTO) {
        EmpQueryDTO empQueryDTO = new EmpQueryDTO();
        for (EmpDTO emp : empMapper.select(empQueryDTO)) {
            if (emp.getUserName().equals(empAddDTO.getUserName())) {
                return -1;
            }
        }

        int insert = empMapper.insert(empAddDTO);
        return insert;
    }

    @Override
    public EmpEditDTO selectById(BigInteger id) {
        return empMapper.selectById(id);
    }

    @Override
    @Transactional
    public int update(EmpEditDTO empEditDTO) {
        for (Emp emp : empMapper.selectNotIncludeId(empEditDTO.getUuid())) {
            if (emp.getUsername().equals(empEditDTO.getUserName())) {
                return -1;
            }
        }
        return empMapper.update(empEditDTO);
    }

    @Override
    public boolean checkLogin(String username, String pwd, HttpSession session) {
        String md5Pwd = generateSaltPassword(pwd);
        Integer empId = empMapper.getEmpInfo(username, md5Pwd);
        if (empId != null) {
            session.setAttribute("username", username);
            session.setAttribute("empId", empId);
            List<String> empMenu = menuMapper.getEmpMenu(empId);
            StringBuilder stringBuilder = new StringBuilder();
            for (String menu : empMenu) {
                stringBuilder.append("/index.html#/");
                stringBuilder.append(menu);
            }
            stringBuilder.append("/index.html#/");
            session.setAttribute("empMenu",stringBuilder.toString());
            return true;
        }
        return false;

    }

    @Override
    public int updatePwd(String oldPwd, String newPwd, HttpSession session) {
        String updateOldPwd = EncryptionPassWord.generateSaltPassword(oldPwd);
        if (newPwd.isEmpty()) {
            return 0;
        }
        int count = empMapper.selectByOldPwd(session.getAttribute("username").toString(),
                updateOldPwd);
        if (count == 0) {
            return -1;
        }
        String updateNewPwd = EncryptionPassWord.generateSaltPassword(newPwd);
        empMapper.updateByNewPwd(session.getAttribute("username").toString(), updateNewPwd);
        return 1;
    }


}
